package gen

import (
	"net/http"
)

type HandlerFunc func(*Context)

type Engine struct {
	router *router
}

//返回一个框架引擎,指针类型
func New() *Engine {
	return &Engine{router: newRouter()}
}

// 提供默认 web 服务
func (self *Engine) Run(addr string) error {
	return http.ListenAndServe(addr, self)
}

func (self *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	ctx := newContext(w, r)
	self.router.handler(ctx)

}

// 增加路由
func (self *Engine) addRouter(method string, path string, handler HandlerFunc) {
	self.router.addRouter(method, path, handler)
}

// 提供四种 http 访问方法
func (self *Engine) GET(path string, handler HandlerFunc) {
	self.addRouter("GET", path, handler)
}

func (self *Engine) POST(path string, handler HandlerFunc) {
	self.addRouter("POST", path, handler)
}

func (self *Engine) PUT(path string, handler HandlerFunc) {
	self.addRouter("PUT", path, handler)
}

func (self *Engine) DELETE(path string, handler HandlerFunc) {
	self.addRouter("DELETE", path, handler)
}
